@echo off
setlocal enabledelayedexpansion

:: 设置源文件夹和目标文件夹的路径
@REM set "sourceFolder=E:\test\"
@REM set "targetFolder=E:\test\to"

set "sourceFolder=%~dp0"
set "targetFolder=%~dp0to"
set "myFileName=%~nx0"
set "count=0"

:: 创建目标文件夹结构
if not exist "%targetFolder%" (
    echo no folder,creating ...
    mkdir "%targetFolder%"
) 

:: 遍历源文件夹中的所有文件
for %%F in ("%sourceFolder%*") do (

    if /i "%%~nxF" NEQ "!myFileName!" (

        set "filename=%%~nF"
        set "extension=%%~xF"
        set "dateString=%%~tF"
        @REM echo !filename!
        @REM echo !extension!
        @REM echo !dateString!

        @REM 提取年份和月份
        for /f "tokens=1-3 delims=/- " %%D in ("!dateString!") do (
            set "year=%%D"
            set "month=%%E"
            set "day=%%F"
            @REM echo !year!
            @REM echo !month!
            @REM echo !day!
        )

        @REM 创建或检查年月日文件夹
        set "yearFolder=%targetFolder%\!year!"
        if not exist "!yearFolder!" mkdir "!yearFolder!"
        set "monthFolder=%targetFolder%\!year!\!month!"
        if not exist "!monthFolder!" mkdir "!monthFolder!"

        move "%%F" "!yearFolder!\!month!"
        echo file !filename! success move to "!yearFolder!\!month!"
    )
)

echo Done.
pause
